<?php
$current_path = dirname(__FILE__);
$project_path = dirname(__FILE__).'/../../tt/';
$project_setup_path = dirname(__FILE__).'/../../tt_setup/files/';
$package_name = 'tt_1_0_0_1.ttPackage';
$package_dir = $current_path.'/../../install/';
$package_path = $package_dir.$package_name;

if(file_exists($package_path))
{
	unlink($package_path);
}

//--------------------------------------
//Fichier a extraire
//--------------------------------------

file_put_contents(dirname(__FILE__).'/index.xml', '<?xml version=\'1.0\' standalone=\'yes\'?>
<ttsetup version="1.0">
	<application>
		<name>TicketTracking</name>
		<version>1.0.0.1</version>
		<author>Silver</author>
		<startpath>./view/</startpath>
	</application>
	<screens css="tt_css.css">
		<requirement type="engine" name="php" minversion="5.0" />
		<requirement type="sgdb" name="mysql" minversion="4.0" />
		<requirement type="phpext" name="zlib" />
		<requirement type="phpext" name="curl" />
		<requirement type="phpext" name="mhash" />
		<requirement type="phpext" name="mcrypt" />
		<requirement type="phpext" name="mysql" />
		<licence>
			bla bla soient d\'accord avec moi et je te benis
		</licence>
		<connection type="mysql" />
		<connection type="root" />
		<extractfile tarpath="extract" serverpath=""/>
		<createtable path="tt.sql" />
	</screens>
	<scripts>
		<aftercopy><![CDATA[
mkdirTree($current_path, \'/cache/smarty/cache\');
mkdirTree($current_path, \'/cache/smarty/template_c\');
createFile(\'conf/bdd.conf.php5\', \'<?php
	$conf[\\\'bdd\\\'][\\\'host\\\'] = \\\'\'.$_SESSION[\'conf_db\'][\'host\'].\'\\\';
	$conf[\\\'bdd\\\'][\\\'login\\\'] = \\\'\'.$_SESSION[\'conf_db\'][\'login\'].\'\\\';
	$conf[\\\'bdd\\\'][\\\'password\\\'] = \\\'\'.$_SESSION[\'conf_db\'][\'password\'].\'\\\';
	$conf[\\\'bdd\\\'][\\\'database\\\'] = \\\'\'.$_SESSION[\'conf_db\'][\'database\'].\'\\\';
	$conf[\\\'bdd\\\'][\\\'prefix\\\'] = \\\'\'.$_SESSION[\'conf_db\'][\'prefix\'].\'\\\';
?>\');
createFile(\'conf/login.conf.php5\', \'<?php
	$conf[\\\'login\\\'][\\\'gmail\\\'][\\\'service\\\'] = \\\'xapi\\\';
	$conf[\\\'login\\\'][\\\'gmail\\\'][\\\'source_company\\\'] = \\\'Silver\\\';
	$conf[\\\'login\\\'][\\\'gmail\\\'][\\\'source_application\\\'] = \\\'TicketTracking\\\';
	$conf[\\\'login\\\'][\\\'gmail\\\'][\\\'source_version\\\'] = \\\'1.0.0.1\\\';
?>\');
createFile(\'conf/setup.conf.php5\', \'<?php
	$conf[\\\'setup\\\'][\\\'host\\\'] = \\\'\'.INSTALL_PACKAGE_HOST.\'\\\';
?>\');
executeSql(\'INSERT INTO \'.$_SESSION[\'conf_db\'][\'prefix\'].\'user_auth SET ua_s_login=\\\'\'.$_SESSION[\'conf_root\'][\'login\'].\'\\\', ua_s_password=\\\'\'.md5($_SESSION[\'conf_root\'][\'password\']).\'\\\', ua_d_created=NOW(), ua_d_updated=NOW()\');
		]]></aftercopy>
	</scripts>
</ttsetup>');

$link = @mysql_connect('localhost', 'root', '');
mysql_select_db('bt', $link);
$sql = 'SHOW TABLES FROM bt';
$res_table = mysql_query($sql, $link);
$sqls = '';
while($row = mysql_fetch_row($res_table))
{
	$sqls .= 'DROP TABLE IF EXISTS `'.$row[0].'`;
';
	$sql = 'SHOW CREATE TABLE `'.$row[0].'`';
	$res_create = mysql_query($sql, $link);
	if($row = mysql_fetch_row($res_create))
	{
		$sqls .= $row[1].';

';
	}
	
	mysql_free_result($res_create);
}
mysql_free_result($res_table);

$tables = array('framework_country');
foreach ($tables as $table)
{
	$sql = 'SELECT * FROM `'.$table.'`';
	$res_data = mysql_query($sql, $link);
	$head = '';
	$body = array();
	while($row = mysql_fetch_assoc($res_data))
	{
		$fields = array_keys($row);
		if($head=='')
		{
			$head = 'INSERT INTO `'.$table.'` 
(`'.implode('`, `', $fields).'`)
VALUES
';
		}
		$sql = '(';
		for($i=0; $i<count($fields); $i++)
		{
			$field = $fields[$i];
			$sql.='\''.addslashes($row[$field]).'\',';
		}
		$body[] = substr($sql, 0, -1).')';
	}
	mysql_free_result($res_data);
	for ($i=0; $i<count($body); $i+=50)
	{
		$sqls .= $head.implode(',
', array_slice($body, $i, 50)).';
';
	}
	$sqls .= '
';
}
file_put_contents(dirname(__FILE__).'/tt.sql', $sqls);


include('./class_archive.php5');
$gzip = new gzip_file($package_name);

$gzip->set_options(array('basedir' => $package_dir, 'level' => 9));

$gzip->add_files('includes.php5', $project_path.'/', 'extract/');
$gzip->add_files('*', $project_path.'/class', 'extract/class/');
$gzip->add_files('*', $project_path.'/lang', 'extract/lang/');
$gzip->add_files('index.php5', $project_path.'/view/', 'extract/view/');
$gzip->add_files('wll_handler.php5', $project_path.'/view/', 'extract/view/');
$gzip->add_files('*', $project_path.'/view/action', 'extract/view/action/');
$gzip->add_files('*', $project_path.'/view/css', 'extract/view/css/');
$gzip->add_files('*', $project_path.'/view/js', 'extract/view/js/');
$gzip->add_files('*', $project_path.'/view/screen', 'extract/view/screen/');
$gzip->add_files('*', $project_path.'/view/extrat', 'extract/view/extrat/');
$gzip->add_files('*', $project_setup_path, 'setup/');
$gzip->add_files('index.xml', dirname(__FILE__), '');
$gzip->add_files('tt.sql', dirname(__FILE__), '');

$gzip->create_archive();

unlink(dirname(__FILE__).'/index.xml');
unlink(dirname(__FILE__).'/tt.sql');

?>